package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.ProcessPaidRecord;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author codingyz
 * @Description
 * @date 2025/3/30 15:03
 * @Version 1.0
 */
@Service("1001")
@Slf4j
public class AlbumItemType implements ProcessPaidRecord {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public void processDiffItemTypePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        Long userId = userPaidRecordVo.getUserId();
        String orderNo = userPaidRecordVo.getOrderNo();
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();

        // 1. 幂等性校验
        Long count = userPaidAlbumMapper.selectCount(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getOrderNo, orderNo).eq(UserPaidAlbum::getUserId, userId));
        if (count != 0) {
            // 已经存过了，直接返回
            log.error("该专辑订单流水已经记录");
            return;
        }
        // 2. 保存订单流水
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setOrderNo(orderNo);
        userPaidAlbum.setUserId(userId);
        userPaidAlbum.setAlbumId(itemIdList.get(0));
        int insert = userPaidAlbumMapper.insert(userPaidAlbum);
        log.info("保存专辑订单流水{}", insert == 0 ? "失败" : "成功");
    }
}
